package com.tpp.threat_perception_platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tpp.threat_perception_platform.dao.ServiceMapper;
import com.tpp.threat_perception_platform.param.MyParam;
import com.tpp.threat_perception_platform.pojo.Service;
import com.tpp.threat_perception_platform.response.ResponseResult;
import com.tpp.threat_perception_platform.service.RiskAssessmentService;
import com.tpp.threat_perception_platform.service.ServiceService;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@org.springframework.stereotype.Service
public class ServiceServiceImpl implements ServiceService {
    @Autowired
    private ServiceMapper serviceMapper;

    @Autowired
    private RiskAssessmentService riskAssessmentService;

    @Transactional
    @Override
    public int saveService(Service service) {
        int count = serviceMapper.countByMacAddress(service.getMacAddress());
        // 设置公共属性
        service.setCreateTime(new Timestamp(System.currentTimeMillis()));
        // 插入新记录
        return serviceMapper.insertSelective(service);
    }

    @Override
    public void deleteByMacAddress(String macAddress) {
        serviceMapper.deleteByMacAddress(macAddress);
    }

    @Override
    public ResponseResult serviceList(MyParam param) {

        // 设置分页参数
        PageHelper.startPage(param.getPage(), param.getLimit());

        // 查询所有
        List<Service> dataList = serviceMapper.findAll(param);

        for (Service service : dataList) {
            String riskLevel = riskAssessmentService.assess(service);
            // 调用评估方法
            service.setRiskLevel(riskLevel);
        }

        // 构架pageInfo
        PageInfo<Service> pageInfo = new PageInfo<>(dataList);

        return new ResponseResult<>(pageInfo.getTotal(), pageInfo.getList());

    }
}
